package api

import (
	"fmt"
	"longmen/server/config/global"
	"longmen/server/pkg/common/helper/const"
	string2 "longmen/server/pkg/common/helper/string"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/grpc_client"
	pb "longmen/server/pkg/protobuf"
	"time"

	"github.com/labstack/echo/v4"
	"longmen/server/pkg/common/api_models"
)

// @Summary 关注、取消关注主播
// @Description 关注、取消关注主播
// @Accept  json
// @Produce  json
// @Tags Anchor
// @Param   sendBody body api_models.AttentanchorReq true "body"
// @Success 200 {object} api_models.AttentanchorResp "result"
// @Router /api/anchor/attentAnchor [POST]
func (base *Controller) AttentAnchor(c echo.Context) error {
	r := &api_models.AttentanchorReq{}
	if err := util.Json2Struct(c.Get("body").(string), r); err != nil {
		return util.BadParam(c, err)
	}
	if err := _const.Validate.Struct(r); err != nil {
		return util.BadParam(c, err)
	}
	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}

	if r.Anchorid == user.Id {
		return util.BuildFail(c, 1, global.Lang.T("cannot_follow_yourself"), nil)
	}
	msg, resp, err := base.Service.AttentAnchor(r, user)
	if err != nil {
		return util.BuildFail(c, 1, msg, err)
	}
	if resp.Type == 0 {
		ctx, cancel := base.GrpcWithTimeout()
		defer cancel()
		_, err = grpc_client.GrpcTencentImClient.QuitGroupAfterUnsubscribe(ctx, &pb.QuitGroupAfterUnsubscribeReq{
			AnchorId: uint32(r.Anchorid),
			UserId:   uint64(user.Id),
		})
		if err != nil {
			global.Log.Error(err)
		}
	}
	return util.BuildSuccess(c, resp, "")
}

// @Summary 批量关注主播
// @Description 批量关注主播
// @Accept  json
// @Produce  json
// @Tags Anchor
// @Param   sendBody body api_models.AttentanchorsReq true "body"
// @Success 200 {object} api_models.Response "result"
// @Router /api/anchor/attentAnchors [POST]
func (base *Controller) AttentAnchors(c echo.Context) error {
	r := &api_models.AttentanchorsReq{}
	if err := util.Json2Struct(c.Get("body").(string), r); err != nil {
		return util.BadParam(c, err)
	}
	if err := _const.Validate.Struct(r); err != nil {
		global.Log.Errorf("6AttentAchors9参数错误%v", err)
		return util.BadParam(c, err)
	}
	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	if len(r.Anchorids) == 0 {
		return util.BuildFail(c, 1, "null list", nil)
	}
	n, err := base.Service.AttentAnchors(r, user)
	if err != nil {
		return util.BuildFail(c, 1, global.Lang.T("follow_failed"), err)
	}
	msg, err := util.CheckClickTime(user.Id, 4, time.Duration(5)*time.Second, time.Duration(1)*time.Minute)
	if err != nil {
		return util.BuildFail(c, 1, msg, err)
	}
	return util.BuildSuccess(c, fmt.Sprintf(global.Lang.T("successfully_followed_anchor"), n), "")
}

func (base *Controller) GetAnchorInfo(c echo.Context) error {
	r := &api_models.GetAnchorInfoReq{}
	if err := util.Json2Struct(c.Get("body").(string), r); err != nil {
		return util.BadParam(c, err)
	}
	if err := _const.Validate.Struct(r); err != nil {
		return util.BadParam(c, err)
	}
	user, _ := base.GetUserByCtx(c)
	msg, resp, err := base.Service.GetAnchorInfo(r, user)
	if err != nil {
		return util.BuildFail(c, 1, msg, err)
	}
	return util.BuildSuccess(c, resp, "")
}

// @Summary 检测主播是否存在
// @Description 检测主播是否存在
// @Accept  json
// @Produce  json
// @Tags Anchor
// @Param   sendBody body api_models.CheckAnchorExistReq true "body"
// @Success 200 {object} api_models.Response "result"
// @Router /api/anchor/checkAnchorExist [POST]
func (base *Controller) CheckAnchorExist(c echo.Context) error {

	r := &api_models.CheckAnchorExistReq{}
	if err := util.Json2Struct(c.Get("body").(string), r); err != nil {
		return util.BadParam(c, err)
	}

	if err := _const.Validate.Struct(r); err != nil {
		return util.BadParam(c, err)
	}

	err := base.Service.CheckAnchorExist(r)
	if err != nil {
		return util.BuildFail(c, 1, err.Error(), err)
	}
	return util.BuildSuccess(c, true, "主播存在")
}

// @Summary 检测是否已关注
// @Description 检测是否已关注
// @Accept  json
// @Produce  json
// @Tags Anchor
// @Param   sendBody body api_models.CheckAttentReq true "body"
// @Success 200 {object} api_models.Response "result"
// @Router /api/anchor/checkAttent [POST]
func (base *Controller) CheckAttent(c echo.Context) error {
	r := &api_models.CheckAttentReq{}
	if err := util.Json2Struct(c.Get("body").(string), r); err != nil {
		return util.BadParam(c, err)
	}
	if err := _const.Validate.Struct(r); err != nil {
		return util.BadParam(c, err)
	}
	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	attend, err := base.Service.CheckAttent(r, user)
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	attended := 0
	if attend.Id > 0 {
		attended = 1
	}
	return util.BuildSuccess(c, map[string]interface{}{"attented": attended}, "")
}

// @Summary 主播基础信息
// @Description 主播基础信息
// @Accept  json
// @Produce  json
// @Tags Anchor
// @Param   sendBody body api_models.GetAnchorBasicInfoReq true "body"
// @Success 200 {object} api_models.GetAnchorBasicInfoResp "result"
// @Router /api/anchor/getAnchorBasicInfo [POST]
func (base *Controller) GetAnchorBasicInfo(c echo.Context) error {

	r := &api_models.GetAnchorBasicInfoReq{}
	if err := util.Json2Struct(c.Get("body").(string), r); err != nil {
		return util.BadParam(c, err)
	}
	if err := _const.Validate.Struct(r); err != nil {
		return util.BadParam(c, err)
	}

	msg, resp, err := base.Service.GetAnchorBasicInfo(r)
	if err != nil {
		return util.BuildFail(c, 1, msg, err)
	}
	return util.BuildSuccess(c, resp, "")
}

func (base *Controller) AddVisitorLog(c echo.Context) error {

	r := &api_models.AddVisitorLogReq{}
	if err := util.Json2Struct(c.Get("body").(string), r); err != nil {
		return util.BadParam(c, err)
	}

	if err := _const.Validate.Struct(r); err != nil {
		return util.BadParam(c, err)
	}

	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	err = base.Service.AddVisitorLog(r, user)
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, nil, "")
}

// @Summary 直播关注列表
// @Description 直播关注列表
// @Accept  json
// @Produce  json
// @Tags Anchor
// @Param   sendBody body api_models.GetAttentAnchorsReq true "body"
// @Success 200 {object} api_models.GetAttentAnchorsResp "result"
// @Router /api/anchor/getAttentAnchors [POST]
func (base *Controller) GetAttentAnchors(c echo.Context) error {

	r := &api_models.GetAttentAnchorsReq{}
	_ = util.Json2Struct(c.Get("body").(string), r)
	r.Page = string2.GetDefaultNum(r.Page, 1)
	r.Size = string2.GetDefaultNum(r.Size, 20)

	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	resp, err := base.Service.GetAttentAnchors(r, user)
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, resp, "")
}

// @Summary 搜索
// @Description
// @Accept  json
// @Produce  json
// @Tags Anchor
// @Param   sendBody body api_models.AnchorSearchReq true "body"
// @Success 200 {object} api_models.Anchor "result"
// @Router /api/anchor/search [POST]
//func (this *Controller) AnchorSearch(c echo.Context) error {
//
//	r := &api_models.AnchorSearchReq{}
//	if err := util.Json2Struct(c.Get("body").(string), r); err != nil {
//		return common.BadParam(c, err)
//	}
//	if err := util.Validate.Struct(r); err != nil {
//		return common.BadParam(c, err)
//	}
//	resp, err := this.Service.AnchorSearch(r)
//	if err != nil {
//		return common.BuildFail(c, 1, "", err)
//	}
//	return common.BuildSuccess(c, resp, "")
//}
